package com.barunsw.bwf.ems.neoarena.ne.tamms.sfh;


public class SfhControl implements SFHConstants {
	final int   ERROR_LENGTH = 128;
	java.io.DataInputStream stream = null;

	public SfhControl(char[] data) {
		try {
			byte[] newData = new byte[data.length];
			for ( int i = 0; i < data.length; i++ )
				newData[i] = (byte)data[i];

			stream = new java.io.DataInputStream(new java.io.ByteArrayInputStream(newData));

		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	public M2000_MODE getDisSlotSTS(){
		M2000_MODE mode = new M2000_MODE();
		byte[] name = new byte[8];

		try{
			mode.cnt = stream.readInt();
			for(int i = 0; i <  mode.SFH_MAX_SLOT_NO; i++){
				mode.slot[i] = new MMC_SLOT_INT_t();	
				mode.slot[i].id = stream.readInt();
				stream.read(name, 0, name.length);
				mode.slot[i].name = (new String(name)).trim();
				mode.slot[i].admin = stream.readInt();
				mode.slot[i].sts = stream.readInt();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M2100_MODE getDisLoadSts(){
		M2100_MODE mode = new M2100_MODE();
		try{
			mode.type = stream.readInt();
			mode.cpu.RsrcUsageThresHold = stream.readInt();
			mode.cpu.RsrcTotalLoad = stream.readInt();
			mode.cpu.RsrcSystemLoad = stream.readInt();
			mode.cpu.RsrcUserLoad = stream.readInt();
			mode.cpu.RsrcOverLoadCount = stream.readInt();

			mode.mem.RsrcUsageThresHold = stream.readInt();
			mode.mem.RsrcCurrentUsage = stream.readInt();
			mode.mem.RsrcTotalStorage = stream.readInt();
			mode.mem.RsrcUsedStoreage = stream.readInt();
			mode.mem.RsrcFreeStorage = stream.readInt();
			mode.mem.RsrcOverLoadCount = stream.readInt();

			mode.dp.Result = stream.readInt();
			mode.dp.CpuLoad = stream.readInt();
			mode.dp.TotalMemSize = stream.readInt();
			mode.dp.UsedMemSize = stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M2710_MODE getDisOvldCfg(){
		M2710_MODE mode = new M2710_MODE();
		try{
			mode.cnt = stream.readInt();
			for(int i=0; i<mode.ovld.length; i++){

				mode.ovld[i] = new OvldData_M();
				mode.ovld[i].kind = stream.readInt();
				mode.ovld[i].threshold = stream.readInt();
				mode.ovld[i].low_norminal = stream.readInt();
				mode.ovld[i].high_norminal = stream.readInt();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M2101_MODE getBdSts(){
		M2101_MODE mode = new M2101_MODE();
		byte[] name = new byte[8];

		try{
			mode.cnt = stream.readInt();

			mode.board = new DisBdSts_M[mode.cnt];
			for(int i=0; i<mode.board.length; i++){
				mode.board[i] = new DisBdSts_M();
				
				mode.board[i].id = stream.readInt();
				stream.read(name, 0, name.length);
				mode.board[i].name = (new String(name)).trim();
				for(int j=0; j<mode.board[i].version.length; j++)
					mode.board[i].version[j] = stream.readInt();
				mode.board[i].act = stream.readInt();
				mode.board[i].uptime = stream.readInt();
				mode.board[i].avltime = stream.readInt();
				mode.board[i].unavltime = stream.readInt();
				mode.board[i].ads = (int)stream.readByte();
				mode.board[i].ops = (int)stream.readByte();
				mode.board[i].alm = (int)stream.readByte();
				mode.board[i].svc = (int)stream.readByte();
				mode.board[i].dp_sts = stream.readInt();

				for(int j=0; j<mode.board[i].dsp_sts.length; j++)
					mode.board[i].dsp_sts[j] = stream.readInt();
			}

			for(int i=0; i<mode.sysled.length; i++)
				mode.sysled[i] = stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}


	public M2200_MODE getDisPortSts(){
		M2200_MODE mode = new M2200_MODE();
		byte[] name = new byte[12];
		try {
			mode.type = stream.readInt();
			mode.cnt = stream.readInt();
			for ( int i = 0; i < mode.l3.length; i++ ) {
				mode.l3[i] = new SwPortSts_M();
				mode.l3[i].port_id = stream.readInt();
				stream.read(name, 0, name.length);
				mode.l3[i].port_if = (new String(name)).trim();
				stream.read(name, 0, name.length);
				mode.l3[i].port_name = (new String(name)).trim();
				mode.l3[i].status.enable = stream.readInt();
				mode.l3[i].status.linkstatus = stream.readInt();
				mode.l3[i].status.autoneg = stream.readInt();
				mode.l3[i].status.speed = stream.readInt();
				mode.l3[i].status.duplex = stream.readInt();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M2216_MODE getDisClkCfg(){
		M2216_MODE mode = new M2216_MODE();
		try{
			mode.mode =  stream.readInt();
			mode.master =  stream.readInt();
			mode.slaver =  stream.readInt();
			mode.master_status =  stream.readInt();
			mode.slave_status =  stream.readInt();
			mode.status =  stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M2222_MODE getDisE1Mode(){
		M2222_MODE mode = new M2222_MODE();
		try{
			for(int i=0; i<mode.link.length; i++){
				mode.link[i].id = stream.readInt();
				mode.link[i].ads = stream.readInt();
				mode.link[i].mode = stream.readInt();
				mode.link[i].crc = stream.readInt();
				mode.link[i].ais = stream.readInt();
				mode.link[i].lbmode = stream.readInt();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}


	public M2300_MODE getDisDspSts(){
		M2300_MODE mode = new M2300_MODE();
		try{
			for(int i=0; i<mode.bd_id.length; i++)
				mode.bd_id[i] = stream.readInt();

			for(int i=0; i<2; i++)
				for(int j=0; j<SFH_MAX_DSP_NO; j++)
					mode.sts[i][j] = stream.readInt();

			for(int i=0; i<2; i++)
				for(int j=0; j<SFH_MAX_DSP_NO; j++)
					mode.ads[i][j] = stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}


	public M2400_MODE getDisRouteInf(){
		M2400_MODE mode = new M2400_MODE();
		byte[] name = new byte[16];
		try{
			mode.cnt = stream.readInt();
System.out.println("[M2400] cnt : " + mode.cnt);

			for(int i=0; i<mode.route.length; i++){
				stream.read(name, 0, name.length);
				mode.route[i].dest = (new String(name)).trim();

				stream.read(name, 0, name.length);
				mode.route[i].gateway = (new String(name)).trim();

				stream.read(name, 0, name.length);
				mode.route[i].genmask = (new String(name)).trim();

				stream.read(name, 0, name.length);
				mode.route[i].flags = (new String(name)).trim();

				mode.route[i].prefix = stream.readInt();
				mode.route[i].metric = stream.readInt();
				mode.route[i].ref = stream.readInt();
				mode.route[i].use = stream.readInt();

				stream.read(name, 0, name.length);
				mode.route[i].iface = (new String(name)).trim();

System.out.println("[M2400] dest : " + mode.route[i].dest);
System.out.println("[M2400] gateway : " + mode.route[i].gateway);
System.out.println("[M2400] genmask : " + mode.route[i].genmask);
System.out.println("[M2400] flags : " + mode.route[i].flags);
System.out.println("[M2400] prefix : " + mode.route[i].prefix);
System.out.println("[M2400] metric : " + mode.route[i].metric);
System.out.println("[M2400] ref : " + mode.route[i].ref);
System.out.println("[M2400] use : " + mode.route[i].use);
System.out.println("[M2400] iface : " + mode.route[i].iface);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M2403_MODE getSysIp(){
		M2403_MODE mode = new M2403_MODE();
		byte[] name = new byte[16];
		try{
			mode.cnt = stream.readInt();
			for(int i=0; i<mode.inf.length; i++){
				mode.inf[i] = new DisSysIp_M();

				stream.read(name, 0, name.length);
				mode.inf[i].Ifname = (new String(name)).trim();

				stream.read(name, 0, name.length);
				mode.inf[i].ipaddr = (new String(name)).trim();

				stream.read(name, 0, name.length);
				mode.inf[i].genmask = (new String(name)).trim();

				stream.read(name, 0, name.length);
				mode.inf[i].defRouter = (new String(name)).trim();

				stream.read(name, 0, name.length);
				mode.inf[i].rtemode = (new String(name)).trim();

				stream.read(name, 0, name.length);
				mode.inf[i].actMode = (new String(name)).trim();

				mode.inf[i].prefix = stream.readInt();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}


	public M2405_MODE getDisIfInf(){
		M2405_MODE mode = new M2405_MODE();
		byte[] name = new byte[16];
		byte[] mac = new byte[20];
		try{
			mode.cnt = stream.readInt();
			for(int i=0; i<mode.itf.length; i++){
				mode.itf[i].ifIndex = stream.readInt();
				mode.itf[i].ifType = stream.readInt();
				stream.read(name, 0, name.length);
				mode.itf[i].ifName = (new String(name)).trim();
				stream.read(name, 0, name.length);
				mode.itf[i].selfIp = (new String(name)).trim();
				stream.read(name, 0, name.length);
				mode.itf[i].defRouterIp = (new String(name)).trim();
				stream.read(name, 0, name.length);
				mode.itf[i].netMask = (new String(name)).trim();
				stream.read(mac, 0, mac.length);
				mode.itf[i].macAddr = (new String(mac)).trim();
				mode.itf[i].speed = stream.readInt();
				mode.itf[i].duplexMode = stream.readInt();
				mode.itf[i].activeStatus = stream.readInt();
				mode.itf[i].operStatus = stream.readInt();
				mode.itf[i].adminStatus = stream.readInt();
				mode.itf[i].boardCurrStatus = stream.readInt();
				mode.itf[i].boardActiveStatus = stream.readInt();
				mode.itf[i].portRelId = stream.readInt();
				mode.itf[i].parentBdId = stream.readInt();
				mode.itf[i].serviceStatus = stream.readInt();
				mode.itf[i].actsbyMode = stream.readInt();
				mode.itf[i].tag = stream.readInt();
				mode.itf[i].vlanid = stream.readInt();
				mode.itf[i].prefix = stream.readInt();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M2422_MODE getDisEmsSvr(){
		M2422_MODE mode = new M2422_MODE();
		byte[] name = new byte[60];
		byte[] ipaddr = new byte[20];
		try{
			mode.cnt = stream.readInt();
			for(int i=0; i<mode.cnt; i++){
				stream.read(name, 0, name.length);
				mode.ems_svr[i].name  = (new String(name)).trim();
				stream.read(ipaddr, 0, ipaddr.length);
				mode.ems_svr[i].ipaddr = (new String(ipaddr)).trim();
				mode.ems_svr[i].port = stream.readInt();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M2428_MODE getDisNtpInf(){
		M2428_MODE mode = new M2428_MODE();
		byte[] flag = new byte[16];
		byte[] ipaddr = new byte[32];
		try{
			stream.read(ipaddr, 0, ipaddr.length);
			mode.ipaddr = (new String(ipaddr)).trim();
			stream.read(flag, 0, flag.length);
			mode.flag = (new String(flag)).trim();
			stream.read(flag, 0, flag.length);
			mode.ops_flag = (new String(flag)).trim();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M1600_MODE getDisShelf(){
		M1600_MODE mode = new M1600_MODE();
		byte[] nameByte = new byte[8];
		try{
			for(int i=0; i<mode.shelf.length; i++){
				
				stream.read(nameByte, 0, nameByte.length);
				mode.shelf[i].rack_name = (new String(nameByte)).trim();

				stream.read(nameByte, 0, nameByte.length);
				mode.shelf[i].shelf_name = (new String(nameByte)).trim();

				mode.shelf[i].col = stream.readInt();
				mode.shelf[i].col_tot = stream.readInt();
				mode.shelf[i].row = stream.readInt();
				mode.shelf[i].row_tot = stream.readInt();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public String getErrorReason()
	{
		byte[] err = new byte[ERROR_LENGTH];
		try
		{
			stream.read(err, 0, err.length);
			return (new String(err)).trim();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public int close()
	{
		try
		{
			stream.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
}
